package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> page(int page,int size){
        Page<Item> itemPage = itemService.page(new Page<>(page, size), null);
        return new PageDTO<Item>(itemPage.getTotal(),itemPage.getRecords());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
        Item item = itemService.getById(id);
        return item;
    }

    /**
     * 新增
     * @param item
     */
    @PostMapping
    public void add(@RequestBody Item item){
       itemService.save(item);
    }

    /**
     * 商品上架下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void update(@PathVariable Long id,@PathVariable int status){
        itemService.updateItem(id,status);
    }

    /**
     * 删除商品
     * @param id
     */
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
        itemService.removeById(id);
    }
}
